/**
 * 会话页面JS
 */

$(function () {
    //激活页面
    activeMenu("session-manager");

    //Initialize Select2 Elements
    $(".select2").select2({
        minimumResultsForSearch: -1
    });

    //新增按钮
    var addSession = $('#session-add');
    addSession.click(function () {
       $("#add-session-pop").show();
    });

    //选择器
    var selectModel= $('#add-session-model-select');
    var selectPack = $('#add-session-pack-select');
    var selectRe = $('#add-session-re-select');
    var selectPm = $('#add-session-pm-select');

    //规则触发
    var selectRule = $('#add-session-rule-select');
    selectRule.change(function () {
        var status = selectRule.val();

        if(status == 1) {
            selectModel.show();
            selectPack.hide();
            selectRe.hide();
            selectPm.hide();
        }else {
            selectModel.hide();
            selectPack.show();
            selectRe.show();
            selectPm.show();
        }
    });


    //资源图标信息
    var dataMis = [], dataDelay = [], dataShake = [] ,totalPoints = 100;

    //获取CPU画图信息
    function getMisData() {
        if (dataMis.length > 0) {
            dataMis = dataMis.slice(1);
        }
        // Do a random walk
        while (dataMis.length < totalPoints) {
            var prev = dataMis.length > 0 ? dataMis[dataMis.length - 1] : 50,
                y = prev + Math.random() * 10 - 5;

            if (y < 0) {
                y = 0;
            } else if (y > 100) {
                y = 100;
            }
            dataMis.push(y);
        }
        // Zip the generated y values with the x values
        var res = [];
        for (var i = 0; i < dataMis.length; ++i) {
            res.push([i, dataMis[i]]);
        }
        return res;
    }

    //获取内存画图信息
    function getDelayData() {
        if (dataDelay.length > 0) {
            dataDelay = dataDelay.slice(1);
        }
        // Do a random walk
        while (dataDelay.length < totalPoints) {
            var prev = dataDelay.length > 0 ? dataDelay[dataDelay.length - 1] : 50,
                y = prev + Math.random() * 10 - 5;

            if (y < 0) {
                y = 0;
            } else if (y > 100) {
                y = 100;
            }
            dataDelay.push(y);
        }
        // Zip the generated y values with the x values
        var res = [];
        for (var i = 0; i < dataDelay.length; ++i) {
            res.push([i, dataDelay[i]]);
        }
        return res;
    }

    //获取硬盘画图信息
    function getShakeData() {
        if (dataShake.length > 0) {
            dataShake = dataShake.slice(1);
        }
        // Do a random walk
        while (dataShake.length < totalPoints) {
            var prev = dataShake.length > 0 ? dataShake[dataShake.length - 1] : 50,
                y = prev + Math.random() * 10 - 5;

            if (y < 0) {
                y = 0;
            } else if (y > 100) {
                y = 100;
            }
            dataShake.push(y);
        }
        // Zip the generated y values with the x values
        var res = [];
        for (var i = 0; i < dataShake.length; ++i) {
            res.push([i, dataShake[i]]);
        }
        return res;
    }

    //初始化图表
    var sessionChart = $.plot("#session-chart",
        [getMisData(),getDelayData(),getShakeData()],
        {
            grid: {
                borderColor: "#f3f3f3",
                borderWidth: 1,
                tickColor: "#f3f3f3"
            },
            series: {
                shadowSize: 0// Drawing is faster without shadows
            },
            lines: {
                fill: false, //Converts the line chart to area chart
            },
            yaxis: {
                min: 0,
                max: 100,
                show: true
            },
            xaxis: {
                show: true
            }
        }
    );

    //设置图表刷新
    var updateInterval = 1000; //Fetch data ever x milliseconds
    var realtime = "on"; //If == to on then fetch data every x seconds. else stop fetching
    function update() {
        sessionChart.setData([getMisData(),getDelayData(),getShakeData()]);
        // Since the axes don't change, we don't need to call plot.setupGrid()
        sessionChart.draw();
        if (realtime === "on") {
            setTimeout(update, updateInterval);
        }
    }
    //INITIALIZE REALTIME DATA FETCHING
    if (realtime === "on") {
        update();
    }

});